package com.taxi.driver.module.order.pool;

import com.qianxx.utils.RxUtil;
import com.socks.library.KLog;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.amap.AMapManager;
import com.taxi.driver.data.order.OrderRepository;
import com.taxi.driver.module.vo.OrderSummaryVO;

import javax.inject.Inject;

/**
 * Created by FTL
 */
public class PoolPresenter extends BasePresenter implements PoolContract.Presenter {

    private final PoolContract.View mView;
    private final OrderRepository mOrderRepository;
    private final AMapManager mAMapManager;

    private String mAreaCode;

    @Inject
    public PoolPresenter(PoolContract.View view, OrderRepository orderRepository, AMapManager aMapManager) {
        mView = view;
        mOrderRepository = orderRepository;
        mAMapManager = aMapManager;
    }

    @Override
    public void subscribe() {
        if (mFirstSubscribe) {
            mAreaCode = mAMapManager.getLastLocation().getAdCode();
        }

        getList();
    }

    @Override
    public void unsubscribe() {

    }

    @Override
    public void getList() {
        mOrderRepository.getOrderPool(mAreaCode)
                .flatMapIterable(entities -> entities)
                .map(OrderSummaryVO::createFrom)
                .toList()
                .compose(RxUtil.applySchedulers())
                .subscribe(mView::setList, KLog::e);
    }
}
